package com.ruoyi.system.service.impl;

import com.alibaba.excel.EasyExcel;
import com.ruoyi.system.domain.ResultFileDTO;
import com.ruoyi.system.domain.vo.FileUploadVO;
import com.ruoyi.system.domain.vo.FileVO;
import com.ruoyi.system.mapper.FileUploadMapper;
import com.ruoyi.system.service.IUploadFileService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/**
 * @author Ly lilongfei
 * @date 2024/8/27
 * @Description
 */
@Service
public class UploadFileServiceImpl implements IUploadFileService {

    @Autowired
    FileUploadMapper fileUploadMapper;

    /**
     * 接收临时文件转文件
     *
     * @param multipartFile
     * @return
     */
    @Override
    public File multipartFile2File(MultipartFile multipartFile) {
        String path = "C:/Users/Ly/Desktop/demo.xlsx";
        File file = new File(path);
        try {
            if (!file.exists()) {
                file.createNewFile();
            }
            // 底层也是通过io流写入文件file
            FileCopyUtils.copy(multipartFile.getBytes(), file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return file;
    }

    /**
     * 读文件,内容存入List
     *
     * @param file
     * @return
     */
    @Override
    public List<FileVO> readExcel(File file) {
        return EasyExcel.read(file)
                .head(FileVO.class)
                .sheet()
                .doReadSync();
    }

    @Override
    public Integer getPose(Integer targetMachine) {
            if (targetMachine >=1 && targetMachine <= 3) {
                return 1;
            }else if (targetMachine >=4 && targetMachine <= 6) {
                return 2;
            }else if (targetMachine >=7 && targetMachine <= 9) {
                return 3;
            }else if (targetMachine >=10 && targetMachine <= 12) {
                return 4;
            }
            else if (targetMachine >=13 && targetMachine <= 15) {
                return 5;
            }
            return 0;

    }

    /**
     * List中内容存入数据库,事务操作保证原子性
     *
     * @param list
     * @param resultFileDTO
     */
    @Override
    @Transactional
    public void saveDatatoDataBase(List<FileVO> list, ResultFileDTO resultFileDTO) {
        List<FileUploadVO> fileVOList = new ArrayList<>();
        if (resultFileDTO.getGame() == 2) {
            for (FileVO excelRow : list) {
                FileUploadVO fileUploadVO = new FileUploadVO();
                fileUploadVO.setTargetMachine(excelRow.getTargetMachine());
                fileUploadVO.setHit(excelRow.getHit());
                fileUploadVO.setDate(resultFileDTO.getDate());
                fileUploadVO.setAthleteId(resultFileDTO.getAthleteId());
                fileUploadVO.setAthleteName(resultFileDTO.getAthleteName());
                fileUploadVO.setGame(resultFileDTO.getGame());
                fileUploadVO.setType(resultFileDTO.getType());
                Integer targetMachine = excelRow.getTargetMachine();
                fileUploadVO.setPos(getPose(targetMachine));
                fileVOList.add(fileUploadVO);
            }
        } else if (resultFileDTO.getGame() == 1) {
            for (FileVO excelRow : list) {
                FileUploadVO fileUploadVO = new FileUploadVO();
                fileUploadVO.setTargetMachine(excelRow.getTargetMachine());
                fileUploadVO.setHit(excelRow.getHit());
                fileUploadVO.setDate(resultFileDTO.getDate());
                fileUploadVO.setAthleteId(resultFileDTO.getAthleteId());
                fileUploadVO.setAthleteName(resultFileDTO.getAthleteName());
                fileUploadVO.setGame(resultFileDTO.getGame());
                fileUploadVO.setType(resultFileDTO.getType());
                fileUploadVO.setPos(excelRow.getTargetMachine());
                fileVOList.add(fileUploadVO);
            }
        }
        fileUploadMapper.BatchInsert(fileVOList);
    }
}



